<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    :close-on-click-modal="false"
    :append-to-body="true"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="0.8rem">
      <el-form-item label="菜单名称" prop="caidanmc">
        <el-input v-model.trim="form.caidanmc" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="菜单类型">
        <el-radio-group v-model="form.caidanlx">
          <el-radio :label="1">菜单</el-radio>
          <el-radio :label="2">按钮</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model.trim="form.tips" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model.trim="form.sort" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="菜单地址">
        <el-input v-model.trim="form.url" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="菜单状态">
        <el-switch
          v-model="form.flag"
          active-text="启用"
          inactive-text="未启用"
          active-value="2"
          inactive-value="1"
        ></el-switch>
      </el-form-item>
      <el-form-item label="上级菜单">
        <el-cascader
          v-model="form.fid"
          :options="treeList"
          :props="{
            checkStrictly: true,
            value: 'id',
            label: 'caidanmc',
            children: 'childList',
            emitPath: false,
          }"
          clearable
        ></el-cascader>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { saveMenu, updateMenu } from '@/api/user'

  export default {
    name: 'UserManagementEdit',
    props: {
      treeList: {
        type: Array,
        default: function () {
          return []
        },
      },
      currentkey: {
        default: null,
      },
    },
    data() {
      return {
        form: {
          caidanmc: '',
          caidanlx: '',
          tips: '',
          sort: '',
          url: '',
          fid: '0',
        },
        rules: {
          caidanmc: [
            { required: true, trigger: 'blur', message: '请输入菜单名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },

    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '新增'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.form.fid = this.currentkey ? this.currentkey : 0
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title === '新增') {
              const { msg } = await saveMenu(this.form)
              this.$baseMessage(msg, 'success')
            } else {
              const { msg } = await updateMenu(this.form)
              this.$baseMessage(msg, 'success')
            }

            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
<style lang="scss" scoped>
  ::v-deep .el-cascader {
    width: 100%;
  }
</style>
